package com.lovo.dao;

import com.lovo.bean.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO{
    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO userDO = null;
        try {
            statement = con.prepareStatement("select * from t_user where u_account=? and u_pwd=?");
            statement.setString(1,username);
            statement.setString(2,pwd);
            rs = statement.executeQuery();
            if(rs.next()){
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setAccount(rs.getString("u_account"));
                userDO.setPwd(rs.getString("u_pwd"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(con,statement);
        }
        return userDO;
    }

    public static void main(String[] args) {
        UserDAO userDAO = new UserDAOImpl();
        System.out.println(userDAO.getByUsernameAndPwd("admin","123456"));
    }
}
